#include "client1.h"
#include "ui_client1.h"
#include <QHostAddress>
#include <QColor>
#include <QPainter>
#include <QPen>
#include <math.h>
#include <QMessageBox>
#include "qcustomplot.h"


Client1::Client1(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Client1)
{
    ui->setupUi(this);

    setWindowTitle("一阶倒立摆");
    move(900,10);

    tcpSocket = NULL;

    tcpSocket = new QTcpSocket(this);

    connect(tcpSocket, &QTcpSocket::connected,
            [=]()
            {
               ui->textEditRead->append("成功连接");
            }
            );

    connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readMessage()));

    Client1::Plot();
}

Client1::~Client1()
{
    delete ui;
}

void Client1::on_ButtonConnect_clicked()
{
    QString ip = ui->lineEditIP->text();
    qint16 port = ui->lineEditPort->text().toInt();

    tcpSocket->connectToHost(QHostAddress(ip), port);
}

void Client1::on_ButtonSend_clicked()
{
    QString str = ui->textEditWrite->toPlainText();

    tcpSocket->write(str.toStdString().c_str(),strlen(str.toStdString().c_str()));

    tpn = str.toDouble();
}

void Client1::on_pushButton_3_clicked()
{
    tcpSocket->disconnectFromHost();
    tcpSocket->close();

    ui->textEditRead->append("断开连接");
}

void Client1::readMessage(){
    QByteArray array = tcpSocket->readAll();
    QString ss = QVariant(array).toString();
    QStringList list = ss.split(",");

    double a = list[0].toDouble();
    double b = list[1].toDouble();


    ui->textEditReada->append(QString::number(a));

    ui->textEditReadb->append(QString::number(b));

    this->position = a;
    this->theta = b;

    this->repaint();


    for(int i = 0; i<101; i++)
    {
        ui->customPlot->graph(0)->addData(x_time, this->position);
        ui->customPlot->graph(1)->addData(x_time, this->theta);
        ui->customPlot->graph(2)->addData(x_time, this->tpn);
        x_time = x_time + 0.002;
    }

    ui->customPlot->replot();

}

void Client1::paintEvent(QPaintEvent *event){

    Q_UNUSED(event);

    QPainter painter(this);
    painter.begin(this);

    QSize size = this->size();
    int Qwidth = size.width();
    int Qheight = size.height();

    int x_posi = int((((Qwidth-80) / this->MAX_VIEW) * this->position));
    int y_posi = Qheight - this->stick_length-30;
    int rotate_center[2] = {int(x_posi + this->car_size[0]/2), y_posi};



    if (this->position <= this->MAX_VIEW){
        QPen pen;

        pen.setWidth(8);
        pen.setColor(QColor(22,105,29));
        painter.setPen(pen);
        int end_point_x = rotate_center[0] - int(this->stick_length * sin(-this->theta));
        int end_point_y = rotate_center[1] - int(this->stick_length * cos(-this->theta));
        painter.drawLine(rotate_center[0], rotate_center[1], end_point_x, end_point_y);

        pen.setWidth(2);
        pen.setColor(QColor(0, 0, 0));
        painter.setPen(pen);
        painter.setBrush(QColor(139, 105, 20));
        painter.drawRect(x_posi, y_posi, this->car_size[0], this->car_size[1]);

        pen.setWidth(2);
        pen.setColor(QColor(0,0,0));
        painter.setPen(pen);
        painter.setBrush(QColor(139, 105, 20));
        painter.drawEllipse(x_posi+10, y_posi+15, 15, 15);
        painter.drawEllipse(x_posi+55, y_posi+15, 15, 15);

    }



    painter.end();
}

void Client1::on_pushButton_clicked()
{
    ui->textEditReada->clear();
}

void Client1::on_pushButton_2_clicked()
{
    ui->textEditReadb->clear();
}

void Client1::on_pushButton_4_clicked()
{
    ui->textEditRead->clear();
}

void Client1::on_ButtonSend_2_clicked()
{
    if(!(QMessageBox::information(this, tr("退出"), tr("确定退出?"), tr("是"), tr("否"))))
    {
        close();
        QApplication* w1;

        w1->quitOnLastWindowClosed();
    }
}

void Client1::on_pushButton_5_clicked()
{
    this->position = 0;
    this->theta = M_PI;

    this->repaint();

    tcpSocket->write("123456");
}

void Client1::Plot()
{
    //QVector<double> x(101),y(101);


    //y.append(this->position);
    //向绘图区域QCustomPlot(从widget提升来的)添加一条曲线
    ui->customPlot->addGraph();
    ui->customPlot->addGraph();
    ui->customPlot->addGraph();
    //添加数据
    ui->customPlot->graph(0)->setPen(QPen(Qt::blue));
    ui->customPlot->graph(1)->setPen(QPen(Qt::red));
    ui->customPlot->graph(0)->addData(0,0);
    ui->customPlot->graph(1)->addData(0,M_PI);

    ui->customPlot->graph(2)->setPen(QPen(Qt::green));





    ui->customPlot->graph(0)->setName("current position");
    ui->customPlot->graph(1)->setName("theta");
    ui->customPlot->graph(2)->setName("target position");
    ui->customPlot->legend->setVisible(true);

    //设置坐标轴标签名称
    ui->customPlot->xAxis->setLabel("x");
    ui->customPlot->yAxis->setLabel("y");

    //设置坐标轴显示范围,否则我们只能看到默认的范围
    ui->customPlot->xAxis->setRange(0,2000);
    ui->customPlot->yAxis->setRange(-1,6);

    ui->customPlot->replot();
}

void Client1::on_pushButton_6_clicked()
{
    ui->customPlot->graph(0)->data().data()->clear();
    ui->customPlot->graph(1)->data().data()->clear();
    ui->customPlot->graph(2)->data().data()->clear();
    ui->customPlot->replot();
    this->x_time = 0.001;
}
